package com.smart.manager.product.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.smart.common.entities.ajax.AjaxResult;
import com.smart.common.entities.constant.ExceptionTips;
import com.smart.common.entities.constant.SmartConstant;
import com.smart.common.entities.core.Page;
import com.smart.manager.product.entities.body.RBasicPigsty;
import com.smart.manager.product.entities.models.BasicPigsty;
import com.smart.manager.product.repository.BasicPigstyRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;


/**
 * 猪圈资料
 * @author leo
 * @date 2023/09/05
 */
@Api(tags =  "猪圈资料")
@RestController
@RequestMapping(SmartConstant.MANAGER_API_PREFIX + "product/basic/pigsty")
@RequiredArgsConstructor
public class BasicPigstyApi {

    private final BasicPigstyRepository repository;
    
    @GetMapping(value = "pageList")
    @ApiOperation(value = "查询猪圈")
    @SaCheckPermission("product:pigsty:pageList")
    public AjaxResult<Page<BasicPigsty>> pageList(@Validated RBasicPigsty.SearchBody body) {
        Page<BasicPigsty> result = repository.pageList(body);
        return AjaxResult.data(result);
    }

    @ApiOperation(value = "添加猪圈")
    @PostMapping("add")
    @SaCheckPermission("product:pigsty:add")
    public AjaxResult<String> add(@RequestBody @Validated RBasicPigsty.EditBody data) {
        repository.add( data );
        return AjaxResult.success();
    }


    @ApiOperation(value = "修改猪圈")
    @PutMapping("update/{id}")
    @SaCheckPermission("product:pigsty:update")
    public AjaxResult<String> update(@PathVariable Long id, @RequestBody @Validated RBasicPigsty.EditBody data) {
        repository.update( id, data );
        return AjaxResult.success();
    }

    @ApiOperation(value = "初始化猪圈")
    @PutMapping("initialize/{id}")
    @SaCheckPermission("product:pigsty:initialize")
    public AjaxResult<String> initialize(@PathVariable Long id) {
        repository.initialize( id );
        return AjaxResult.success();
    }


    @ApiOperation(value = "删除猪圈")
    @DeleteMapping("delete")
    @SaCheckPermission("product:pigsty:delete")
    public AjaxResult<Integer> delete(@RequestBody @NotEmpty(message = ExceptionTips.PK_EMPTY) List<Long> ids) {
        int i = repository.delByIds( ids );
        return AjaxResult.delSuccess( i );
    }

}
